<template>
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
            <el-col :lg="4" :md="12">
                <el-form-item label="年度:">
                  <el-input
                    clearable
                    v-model="form.yearly"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="4" :md="12">
                <el-form-item label="月度:">
                  <el-input
                    clearable
                    v-model="form.monthly"
                    placeholder=""/>
                </el-form-item>
              </el-col>
                <el-col :lg="4" :md="12">
              <div class="ele-form-actions">
                <el-button
                  type="primary"
                  icon="el-icon-config"
                  class="ele-btn-icon"
                  size="small"
                  @click="syncdata">同步数据
                </el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          :page-size="100"
          height="calc(100vh - 315px)">
          <!-- 表头工具栏 -->
          <template slot="toolbar" v-if="false">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:commercialacquisitionreport:add')">添加
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:commercialacquisitionreport:dall')">删除
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="action" slot-scope="{row}" v-if="false">
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)"
              v-if="permission.includes('sys:commercialacquisitionreport:edit')">修改
            </el-link>
            <el-popconfirm
              class="ele-action"
              title="确定要删除此收购报表吗？"
              @confirm="remove(row)">
              <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                v-if="permission.includes('sys:commercialacquisitionreport:delete')">删除
              </el-link>
            </el-popconfirm>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
  </template>
  
  <script>
  import { mapGetters } from "vuex";
  
  export default {
    name: 'ExamCommercialAcquisitionReport',
    computed: {
      ...mapGetters(["permission"]),
    },
    data() {
      return {
        // 表格数据接口
        url: '/commercialacquisitionreport/index',
        form:{
            yearly:'',
            monthly:'',
        },
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: "left"
          },
          {
            prop: 'id',
            label: 'ID',
            width: 60,
            align: 'center',
            showOverflowTooltip: true,
            fixed: "left"
          },
          {
            prop: 'yearly',
            label: '年度',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'monthly',
            label: '月度',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'purchaseCode',
            label: '采购方编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'saleCode',
            label: '销售方编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'materialCode',
            label: '物料编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'purchaseNum',
            label: '采购量',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'purchaseAmount',
            label: '采购额',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'purchaseMargin',
            label: '采购边际',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'dataSource',
            label: '数据来源',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'bipId',
            label: 'BIP主键',
            showOverflowTooltip: true,
            minWidth: 160,
            align: 'center'
          },
          // {
          //   prop: 'creator',
          //   label: '创建人',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'modifier',
          //   label: '修改人',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'modifyTime',
          //   label: '修改时间',
          //   sortable: 'custom',
          //   showOverflowTooltip: true,
          //   minWidth: 160,
          //   align: 'center',
          //   formatter: (row, column, cellValue) => {
          //     return this.$util.toDateString(cellValue);
          //   }
          // },
          {
            prop: 'createTime',
            label: '创建时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 160,
            align: 'center',
            formatter: (row, column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          // {
          //   columnKey: 'action',
          //   label: '操作',
          //   width: 150,
          //   align: 'center',
          //   resizable: false,
          //   slot: 'action',
          //   fixed: "right"
          // }
        ],
        // 表格搜索条件
        where: {},
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
      };
    },
    methods: {
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({where: this.where});
      },
      syncdata(){
        if(!this.form.yearly)
        {
            this.$message.error('请输入年度后同步收购报表数据，月度为空同步整年数据。');
            return;
        }
        const loading = this.$loading({lock: true});
        this.$http.post('/bipservice/manualsyncreport',this.form).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.reload();
      },
      /* 显示编辑 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({lock: true});
        this.$http.delete('/commercialacquisitionreport/delete', {id: row.id}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的收购报表吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/commercialacquisitionreport/delete', {id: this.selection.map(d => d.id)}).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      },
    }
  }
  </script>
  
  <style scoped>
  </style>
  